Module Name: othersrc
Committed By: dyoung
Date: Wed Dec 2 23:42:45 UTC 2015
Added Files:
othersrc/external/bsd/arfe/dt: sym.c sym.h
Removed Files:
othersrc/external/bsd/arfe/dt/rr: quack123quack-quack456quack
quack456quack-quack123quack quackabequack-quackdariaquack
quackdariaquack-quackabequack
Log Message:
Straggler from last: add sym.[ch]. Remove some files belonging to dead
tests.
To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 othersrc/external/bsd/arfe/dt/sym.c \
othersrc/external/bsd/arfe/dt/sym.h
cvs rdiff -u -r1.2 -r0 \
othersrc/external/bsd/arfe/dt/rr/quack123quack-quack456quack \
othersrc/external/bsd/arfe/dt/rr/quack456quack-quack123quack \
othersrc/external/bsd/arfe/dt/rr/quackabequack-quackdariaquack \
othersrc/external/bsd/arfe/dt/rr/quackdariaquack-quackabequack
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Added files:
Index: othersrc/external/bsd/arfe/dt/sym.c
diff -u /dev/null othersrc/external/bsd/arfe/dt/sym.c:1.1
--- /dev/null Wed Dec 2 23:42:45 2015
+++ othersrc/external/bsd/arfe/dt/sym.c Wed Dec 2 23:42:45 2015
@@ -0,0 +1,215 @@
+/* $NetBSD: sym.c,v 1.1 2015/12/02 23:42:45 dyoung Exp $ */
+/* $ARFE$ */
+
+/*-
+ * Copyright (c) 2014,2015 David Young <[email protected]>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <assert.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "core.h"
+#include "sym.h"
+
+typedef enum sym_op {
+ SYM_OP_NONE = 0
+ , SYM_OP_PUSH
+ , SYM_OP_EMIT
+ , SYM_OP_START
+} sym_op_t;
+
+struct sym_state;
+typedef struct sym_state sym_state_t;
+
+typedef struct sym_transition {
+ const sym_state_t *t_newstate;
+ sym_op_t t_op;
+} sym_transition_t;
+
+struct sym_state {
+ const char *s_descr;
+ sym_transition_t s_other;
+ sym_transition_t s_underscore;
+ sym_transition_t s_0_9;
+ sym_transition_t s_alpha;
+ sym_transition_t s_delimiter;
+ sym_transition_t s_eof;
+};
+
+enum sym_state_idxs {
+ READ_SYM
+ , READ_EOF
+ , READ_OTHER
+ , READ_DELIMITER
+};
+
+#define read_sym sym_states[READ_SYM]
+#define read_eof sym_states[READ_EOF]
+#define read_other sym_states[READ_OTHER]
+#define read_delimiter sym_states[READ_DELIMITER]
+
+static const sym_state_t sym_states[] = {[READ_SYM] = {
+ .s_descr = "read start",
+ .s_other = {&read_other, SYM_OP_NONE},
+ .s_delimiter = {&read_delimiter, SYM_OP_EMIT},
+ .s_0_9 = {&read_sym, SYM_OP_PUSH},
+ .s_alpha = {&read_sym, SYM_OP_PUSH},
+ .s_underscore = {&read_sym, SYM_OP_PUSH},
+ .s_eof = {&read_eof, SYM_OP_EMIT},
+}, [READ_EOF] = {
+ .s_descr = "read eof",
+ .s_other = {&read_eof, SYM_OP_NONE},
+}, [READ_OTHER] = {
+ .s_descr = "read other",
+ .s_other = {&read_other, SYM_OP_NONE},
+ .s_delimiter = {&read_delimiter, SYM_OP_NONE},
+}, [READ_DELIMITER] = {
+ .s_descr = "read delimiter",
+ .s_underscore = {&read_sym, SYM_OP_START},
+ .s_alpha = {&read_sym, SYM_OP_START},
+ .s_delimiter = {&read_delimiter, SYM_OP_NONE},
+ .s_other = {&read_other, SYM_OP_NONE},
+}};
+
+struct sym_parser {
+ sym_detection_t p_detection;
+#define p_column p_detection.d_column
+#define p_chars p_detection.d_chars
+#define p_idx p_detection.d_idx
+ char *p_top;
+ const sym_state_t *p_state;
+ sym_emitter_t p_emitter;
+ void *p_arg;
+};
+
+void
+sym_parser_free(sym_parser_t *p)
+{
+ free(p);
+}
+
+static void
+sym_parser_init(sym_parser_t *p, sym_emitter_t emitter, void *arg)
+{
+ memset(p, 0, sizeof(*p));
+ p->p_state = &read_delimiter;
+ p->p_emitter = emitter;
+ p->p_arg = arg;
+}
+
+sym_parser_t *
+sym_parser_alloc(sym_emitter_t emitter, void *arg)
+{
+ sym_parser_t *p;
+
+ p = malloc(sizeof(*p));
+ if (p == NULL)
+ return NULL;
+
+ sym_parser_init(p, emitter, arg);
+ return p;
+}
+
+static const char *
+sym_op_string(sym_op_t op)
+{
+ switch (op) {
+ case SYM_OP_NONE:
+ return "none";
+ case SYM_OP_EMIT:
+ return "emit";
+ case SYM_OP_PUSH:
+ return "push";
+ case SYM_OP_START:
+ return "start";
+ default:
+ return "unknown";
+ }
+}
+
+void
+sym_parser_drive(sym_parser_t *p, int idx, int column, int c)
+{
+ const sym_transition_t *t;
+ const sym_state_t *ns, *s;
+
+ s = p->p_state;
+
+ if (c == -1 && (ns = s->s_eof.t_newstate) != NULL)
+ t = &s->s_eof;
+ else if (c == '_' && (ns = s->s_underscore.t_newstate) != NULL)
+ t = &s->s_underscore;
+ else if ((isspace(c) || ispunct(c)) && c != '_' &&
+ (ns = s->s_delimiter.t_newstate) != NULL)
+ t = &s->s_delimiter;
+ else if (isalpha(c) && (ns = s->s_alpha.t_newstate) != NULL)
+ t = &s->s_alpha;
+ else if (isdigit(c) && (ns = s->s_0_9.t_newstate) != NULL)
+ t = &s->s_0_9;
+ else if ((ns = s->s_other.t_newstate) != NULL)
+ t = &s->s_other;
+ else
+ ns = NULL;
+
+ assert(ns != NULL);
+ if (c == -1) {
+ dbg_printf("%s: transition (%s, eof) -> %s (op %s)\n", __func__,
+ s->s_descr, ns->s_descr, sym_op_string(t->t_op));
+ } else {
+ dbg_printf("%s: transition (%s, %c) -> %s (op %s)\n", __func__,
+ s->s_descr, c, ns->s_descr, sym_op_string(t->t_op));
+ }
+
+ switch (t->t_op) {
+ case SYM_OP_START:
+ p->p_column.start = column;
+ p->p_idx.start = idx;
+ p->p_top = &p->p_chars[0];
+ /*FALLTHROUGH*/
+ case SYM_OP_PUSH:
+ if (p->p_top == NULL)
+ ;
+ else if (p->p_top - &p->p_chars[0] ==
+ __arraycount(p->p_chars) - 1)
+ p->p_top = NULL;
+ else {
+ *p->p_top = c;
+ p->p_top++;
+ }
+ p->p_column.stop = column;
+ p->p_idx.stop = idx;
+ break;
+ case SYM_OP_EMIT:
+ if (p->p_top == NULL)
+ dbg_printf("symbol too long\n");
+ *p->p_top = '\0';
+ (*p->p_emitter)(&p->p_detection, p->p_arg);
+ break;
+ case SYM_OP_NONE:
+ break;
+ }
+
+ p->p_state = ns;
+}
Index: othersrc/external/bsd/arfe/dt/sym.h
diff -u /dev/null othersrc/external/bsd/arfe/dt/sym.h:1.1
--- /dev/null Wed Dec 2 23:42:45 2015
+++ othersrc/external/bsd/arfe/dt/sym.h Wed Dec 2 23:42:45 2015
@@ -0,0 +1,49 @@
+/* $NetBSD: sym.h,v 1.1 2015/12/02 23:42:45 dyoung Exp $ */
+/* $ARFE$ */
+
+/*-
+ * Copyright (c) 2014,2015 David Young <[email protected]>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef _SYM_H
+#define _SYM_H
+
+#include <stdbool.h>
+
+typedef struct sym_detection {
+ struct {
+ int start, stop;
+ } d_column, d_idx;
+ /* XXX shouldn't limit this like this */
+ char d_chars[64];
+} sym_detection_t;
+
+struct sym_parser;
+typedef struct sym_parser sym_parser_t;
+
+typedef void (*sym_emitter_t)(sym_detection_t *, void *);
+sym_parser_t *sym_parser_alloc(sym_emitter_t, void *);
+void sym_parser_drive(sym_parser_t *, int, int, int);
+void sym_parser_free(sym_parser_t *);
+
+#endif /* _SYM_H */